package com.restfinder.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.restfinder.common.component.JsonResponse;
import com.restfinder.common.component.JsonResponseFactory;
import com.restfinder.common.component.ValidationJsonResponse;
import com.restfinder.common.component.utils.JsonSpringUtils;
import com.restfinder.common.component.utils.MessageUtils;
import com.restfinder.common.component.utils.SecurityUtils;
import com.restfinder.restaurant.model.Restaurant;
import com.restfinder.user.component.PasswordUtils;
import com.restfinder.user.component.UserValidator;
import com.restfinder.user.forms.RestaurantForm;
import com.restfinder.user.forms.UserForm;
import com.restfinder.user.model.User;
import com.restfinder.user.service.UserService;

@Controller
@SessionAttributes("userForm")
@Slf4j
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private UserValidator userValidator;
	
	@InitBinder
	protected void initBinder(WebDataBinder binder){
		binder.setValidator(userValidator);
	}
	
	@RequestMapping(value="/user/{username}", method=RequestMethod.GET)
	public ModelAndView userGet(@PathVariable String username){
		User user = userService.getByUsername(username);
		UserForm uf = new UserForm();
		uf.setUser(user);
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("userForm", uf);
		return new ModelAndView("user/user", modelMap);		
	}
	
	@RequestMapping(value="/user/{username}/account", method=RequestMethod.GET)
	public ModelAndView accountGet(@PathVariable String username){
		String loggedUserUsername = SecurityUtils.getAuthenticatedUsername();
		if(loggedUserUsername.equalsIgnoreCase(username)){
			ModelMap modelMap = new ModelMap();
			UserForm uf = new UserForm();
			User user = userService.getByUsername(username);
			uf.setUser(user);
			uf.setPassword2(user.getPassword());
			modelMap.addAttribute("userForm", uf);
			return new ModelAndView("user/account", modelMap);
		}
		else{
			return new ModelAndView("redirect:/");
		}
	}
	
	@RequestMapping(value="/user/update", method=RequestMethod.POST)
	public @ResponseBody JsonResponse userUpdatePost(@ModelAttribute("userForm") @Valid UserForm userForm, 
			BindingResult result, HttpServletRequest request){
		JsonResponse jsonResponse = JsonResponseFactory.createJsonResponse(result);
		if(!JsonSpringUtils.isSuccessResponse(jsonResponse)){
			jsonResponse.setResult(MessageUtils.createMessage("account.error.title", "account.error.content"));
			return jsonResponse;
		}
		else{
			try {
				userService.update(userForm.getUser());
				jsonResponse.setResult(MessageUtils.createMessage("account.success.title", "account.success.title"));
				return jsonResponse;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("Error while updating user",e);
				jsonResponse = new ValidationJsonResponse();
				jsonResponse.setStatus(JsonSpringUtils.JSON_STATUS_FAILURE);
				jsonResponse.setResult(MessageUtils.createMessage("account.error.title", e));
				return jsonResponse;
			}
			
		}	
	}
	
}
